﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.FTPClient;
using System.Data.OracleClient;
using System.IO;
namespace WPFExcelReport
{
    struct vvPACK
    {
        public string PackNr;
        public SortedList<string, DataRow> WBBSTnr;
    }
    struct vvVKPOS
    {
        public DataRow dwVKPOS;
        public SortedList<string, vvPACK> listVVPACK;
    }
    class DSTGBMOVReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        byte[] b;
        public IntexReptServBLClient proxy;
        public OracleConnection OracleDB;
        string WBBAUFKRS;
        string WBBAUFLFD;
        string WBBAUFPOS;
        string WBBALIEFSCH;
        string VKAUFNum = "";
        string WBBTAX;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string orderComments = "备注：";
        string strExcelFile;
        public string Customer = "";
        public string Address = "";
        string DateCurrent = "";
        public string PrintType = "JustExcel";
        public List<vvVKPOS> listVKPOS = new List<vvVKPOS>();
        public FTPClient ftpClient;
        #endregion
        public DSTGBMOVReport()
            : base()
        {

        }
        public DSTGBMOVReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            DateCurrent = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日";
            #region Excel  columns
            columns.Add("GABBUCHDAT");
            columns.Add("KDKUNDNA");
            columns.Add("CO");
            columns.Add("GABART");
            columns.Add("GABVAR");
            columns.Add("GABFARBE");
            columns.Add("GABPARTIE");
            columns.Add("LSNAME1");
            columns.Add("GPLIEFSCH");
            columns.Add("GPSPPART");
            columns.Add("KG");
            columns.Add("GABSP");
            columns.Add("PRICE");
            columns.Add("VALUE");
            #endregion

        }

        public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["GBMOV"];
                foreach (DataRow dataRow in dtSoruce.Rows)
                {
                    y = yStart;
                    foreach (string column in columns)
                    {
                        exAgent.SetCellValue(x, y, dataRow[column].ToString());
                        y++;
                    }
                    x++;
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        
        public override void WriteExcelReport(string fileType)
        {
            string filename = "";
            string sheetName = "";
            string Standard = "GBMOV";
            strExcelFile = @"DST\DSTInventory.xls";
            string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\DST\DSTInventory.xls");
            exAgent.Open(filePath);
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            filename = @"仓库\纱线出库" + filedate + ".xls";
            sheetName = DateTime.Now.ToShortTimeString().Replace(":", "_").Replace(" ", "_").Replace("/", "_");
            exAgent.CreateNewSheet(filename, Standard, sheetName);
            FillInExcelData(columns, dsSource, 3, 1); 
            exAgent.Save();
            exAgent.Close();
            string files = Path.Combine(PathHelper.ApplicationPath, @"Report\"+filename);
            System.Diagnostics.Process.Start("EXCEL.EXE", files);                
            base.WriteExcelReport(fileType);
        }

    }
}
